<?php
namespace Home\Controller;
use Think\Controller;
use Think\AjaxPage;
use Think\Page;
class ChatController extends Controller {
	public function _initialize() {
		$token = I('token')?I('token'):'';
		if($token){
			$id = M('users')->where(array('token'=>$token))->getField('id');	
			if(!$id){
				exit(json_encode(array('code'=>400,'number'=>rand(1,99),'msg'=>'请先登录','jn_msg'=>'最初にログインしてください')));						
			}else{
				$this->user_id = $id;
			}
		}else{
			exit(json_encode(array('code'=>100,'msg'=>'请先登录','jn_msg'=>'最初にログインしてください')));			
		}
		check_goods();
	}
	public function chat_info(){
		$uid = I('user_id')?I('user_id'):'';
		$user = M('users')->where(array('id'=>$uid))->field('id,realname,username,head_img')->find();
		if($user['head_img']){
			$user['head_img'] = C('WEB_URL').$user['head_img'];
		}else{
			$user['head_img'] = '../images/member.png';
		}
		if($this->user_id!=$uid){
			$user['other_name'] = M('users_chat_list')->where(array('user_id'=>$this->user_id,'form_id'=>$uid))->getField('other_name');
		}
		exit(json_encode(array('code'=>200,'data'=>$user)));		
	}
	public function set_info(){
		$user_id = $this->user_id;
		$uid = I('uid')?I('uid'):'';
		$other_name = I('other_name')?I('other_name'):'';
		$mod = M('users_chat_list');		
		$result = $mod->where(array('user_id'=>$user_id,'form_id'=>$uid))->save(array('other_name'=>$other_name));
		if($result){
			exit(json_encode(array('code'=>200,'data'=>$_POST)));	
		}else{
			exit(json_encode(array('code'=>100,'msg'=>'设置失败','jn_msg'=>'セットアップに失敗しました')));	
		}
	}
	public function chat_read_num(){
		$user_id = $this->user_id;
		$mod = M('users');
		$chat_mod = M('users_chat');
		$w['is_read'] = 0;
		$w['form_id'] = $user_id;
		$num = $chat_mod->where($w)->count();
		exit(json_encode(array('code'=>200,'data'=>$num)));		
	}
	public function chat_firend_list(){
		$user_id = $this->user_id;
		$mod = M('users');
		$chat_mod = M('users_chat');
		$list = M('users_chat_list')->where(array('user_id'=>$user_id,'status'=>1))->order('add_time desc')->select();
		foreach((array)$list as $k=>$v){
			$w['user_id'] = $v['form_id'];
			$w['form_id'] = $user_id;
			$w['is_read'] = 0;
			$u = $mod->where(array('id'=>$v['form_id']))->field('id,username,realname,head_img')->find();
			if($u['head_img']){
				$u['head_img'] = C('WEB_URL').$u['head_img'];
			}else{
				$u['head_img'] = '../images/member.png';
			}
			$name = M('users_chat_list')->where(array('user_id'=>$user_id,'form_id'=>$v['form_id']))->getField('other_name');
			$name && $u['realname'] = $name;
			$u['read_num'] = $chat_mod->where($w)->count();
			
			$chat = $chat_mod->where('(user_id='.$user_id.' and form_id='.$v['form_id'].') or (user_id='.$v['form_id'].' and form_id='.$user_id.')')->order('id desc')->field('content,type_id')->find();			
			$u['content'] = $chat['content'];
			$u['type_id'] = $chat['type_id'];
			$u['time'] = $this->check_time($v['add_time']);
			$arr[] = $u;
		}
		if(count($arr)){
			exit(json_encode(array('code'=>200,'data'=>$arr)));		
		}else{
			exit(json_encode(array('code'=>100,'data'=>$arr)));	
		}
	}
	public function del_firend_list(){
		$user_id = $this->user_id;
		$form_id = I('id')?I('id'):'';
		$mod = M('users_chat_list');
		$result = $mod->where(array('user_id'=>$user_id,'form_id'=>$form_id))->save(array('status'=>0));
		if($result){
			exit(json_encode(array('code'=>200,'data'=>$result)));		
		}else{
			exit(json_encode(array('code'=>100,'data'=>$result)));	
		}
	}
	public function check_time($t){
		$lang = I('lang')?I('lang'):'';
		$now_day = strtotime(date('Y-m-d'));
		$prev_day = strtotime(date("Y-m-d",strtotime("-1 day")));
		if($t>$now_day){
			return date('H:i',$t);
		}else if($t<$now_day && $t>$prev_day){
			return '昨天';
		}else{
			return date('m月d日',$t);
		}
	}
	public function chat_list(){
		$user_id = $this->user_id;		
		$form_id = I('username')?I('username'):'';	
		if($form_id){
			$mod = M('users_chat');	
			$user_mod = M('users');
			$list = $mod->where('(user_id='.$user_id.' and form_id='.$form_id.') or (user_id='.$form_id.' and form_id='.$user_id.')')->order('id asc')->select();
			foreach((array)$list as $k=>$v){
				$user_img = $user_mod->where(array('id'=>$v['user_id']))->getField('head_img');
				$list[$k]['user_img'] = $user_img?C('WEB_URL').$user_img:'../images/member.png';
			}
			$mod->where(array('user_id'=>$form_id,'form_id'=>$user_id))->save(array('is_read'=>1));

			$goods_id = M('users_chat_list')->where(array('user_id'=>$user_id,'form_id'=>$form_id))->getField('goods_id');		
			$detail = array();
			if($goods_id){
				$detail = M('goods')->where(array('id'=>$goods_id))->find();
				$detail['thumb'] && $detail['thumb'] = C('WEB_URL').$detail['thumb'];
			}		
			exit(json_encode(array('code'=>200,'data'=>$list,'goods'=>$detail)));
		}
	}
	public function set_chat(){
		$user_id = $this->user_id;
		$mod = M('users_chat');	
		$form_id = I('username')?I('username'):'';
		$type_id = I('type_id')?I('type_id'):'';
		$content = I('content')?I('content'):'';
		if($form_id){
			$data['user_id'] = $user_id;
			$data['form_id'] = $form_id;
			$data['type_id'] = $type_id;
			$data['content'] = $content;
			$data['add_time'] = time();
			$result = $mod->add($data);
			if($result){
				$this->check_chat($user_id,$form_id);
				exit(json_encode(array('code'=>200,'data'=>$result)));
			}
		}
	}
	public function add_chat(){
		$mod = M('users_chat');	
		$user_id = I('form_name')?I('form_name'):'';
		$form_id = I('username')?I('username'):'';
		$content = I('content')?I('content'):'';
		if($form_id){
			$data['user_id'] = $user_id;
			$data['form_id'] = $form_id;
			$data['content'] = $content;
			$data['add_time'] = time();
			$result = $mod->add($data);
			if($result){
				$this->check_chat($user_id,$form_id);
				exit(json_encode(array('code'=>200,'data'=>$result)));
			}
		}
	}
	public function check_chat($user_id,$form_id){
		$mod = M('users_chat_list');
		$time = time();
		$check = $mod->where(array('user_id'=>$user_id,'form_id'=>$form_id))->getField('id');
		if(!$check){
			$mod->add(array('user_id'=>$user_id,'form_id'=>$form_id,'add_time'=>$time));
			$mod->add(array('user_id'=>$form_id,'form_id'=>$user_id,'add_time'=>$time));
		}else{
			$mod->where(array('user_id'=>$user_id,'form_id'=>$form_id))->save(array('add_time'=>$time,'status'=>1));
			$mod->where(array('user_id'=>$form_id,'form_id'=>$user_id))->save(array('add_time'=>$time,'status'=>1));		
		}
	}
	
	public function check_goods_chat(){
		$user_id = $this->user_id;
		$form_id = I('form_id')?I('form_id'):'';
		$goods_id = I('goods_id')?I('goods_id'):'';
		$mod = M('users_chat_list');
		$time = time();
		$check = $mod->where(array('user_id'=>$user_id,'form_id'=>$form_id))->getField('id');
		if(!$check){
			$mod->add(array('user_id'=>$user_id,'form_id'=>$form_id,'add_time'=>$time,'goods_id'=>$goods_id));
			$mod->add(array('user_id'=>$form_id,'form_id'=>$user_id,'add_time'=>$time,'goods_id'=>$goods_id));
		}else{
			$mod->where(array('user_id'=>$user_id,'form_id'=>$form_id))->save(array('add_time'=>$time,'status'=>1,'goods_id'=>$goods_id));
			$mod->where(array('user_id'=>$form_id,'form_id'=>$user_id))->save(array('add_time'=>$time,'status'=>1,'goods_id'=>$goods_id));		
		}
		exit(json_encode(array('code'=>200,'data'=>$check)));
	}
	
	function upload_sound(){
		$user_id = $this->user_id;
		$mod = M('users');	
		if($_FILES['sound']['name']){		
			$upload = new \Think\Upload();// 实例化上传类
			$upload->maxSize = 3145728;
			$upload->rootPath = './Public/upload/';
			$upload->savePath = 'sound/';
			$upload->saveName = array('uniqid','');
			$upload->exts     = array('amr');
			$upload->autoSub  = false;//子目录
			$upload->subName  = array('Public','Ymd');
			$info   =   $upload->uploadOne($_FILES['sound']);
			if($info){	
				$data['head_img'] = '/Public/upload/sound/'.$info['savename'];
				$id = M('users_chat')->order('id desc')->getField('id');
				exit(json_encode(array('code'=>200,'data'=>$data['head_img'],'id'=>$id+1)));			
			}else{
				
				exit(json_encode(array('code'=>100,'msg'=>'修改失败','jn_msg'=>'変更に失敗しました','data'=>$upload->getError())));			
			}
		}else{
			exit(json_encode(array('code'=>100,'msg'=>'修改失败','jn_msg'=>'変更に失敗しました')));				
		}
	}
	
	function upload_img(){
		$user_id = $this->user_id;
		$mod = M('users');	
		if($_FILES['thumb']['name']){		
			$upload = new \Think\Upload();// 实例化上传类
			$upload->maxSize = 3145728;
			$upload->rootPath = './Public/upload/';
			$upload->savePath = 'chat/';
			$upload->saveName = array('uniqid','');
			$upload->exts     = array('jpg');
			$upload->autoSub  = false;//子目录
			$upload->subName  = array('Public','Ymd');
			$info   =   $upload->uploadOne($_FILES['thumb']);
			if($info){	
				$data['head_img'] = '/Public/upload/chat/'.$info['savename'];
				exit(json_encode(array('code'=>200,'data'=>$data['head_img'])));			
			}else{
				
				exit(json_encode(array('code'=>100,'msg'=>'修改失败','jn_msg'=>'変更に失敗しました','data'=>$upload->getError())));			
			}
		}else{
			exit(json_encode(array('code'=>100,'msg'=>'修改失败','jn_msg'=>'変更に失敗しました')));				
		}
	}
}